<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="4" :labelWidth="110" :formType="type" :isSubmitBtn="false" />
    <BreakSpace label="操作历史" :containerStyle="{ marginBottom: '10px' }" />
    <FilterTable
      ref="table"
      :columns="columns"
      :dataSource="dataList"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="columns => (this.selectColumns = columns)"
      :isSelectColumn="false"
      :isToperInfo="false"
      :isColumnFilter="false" 
      :height="172"
      style="margin-bottom:20px;"
    /> 
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
        <AjaxButton v-if="this.type == 'edit' " type="primary" :click="passClick" >通过</AjaxButton>
        <AjaxButton v-if="this.type == 'edit' " type="primary" :click="turnDownClick">驳回</AjaxButton>
        <el-button @click="closeDialog">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { phone } from '@/utils/validate';
import { region } from '@/api/cms-common/region'; //行政区域
import moment from 'moment';
import { assessorStatus } from '@/api/repair/repairBusiness/rescueService';
import { getProvince, getCitys, getDistrict } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';
import { getOutLog } from '@/api/repair/repairBusiness/rescueService/index.js';
import config from '@/assets/js/config';
import { findFileByUrl } from '@/api/partsmanage/financialManagement/capitalAccount/index'
export default {
  name: 'rescueEditDialog',
  props: {
    formData: {
      type: Object,
      default: () => ({})
    },
    type: {
      type: String
    }
  },
  mixins: [dictionary, region],
  data() {
    return {
      formList: this.createFormList(),
      columns: this.createTableColumns(),
      dataList: []
    };
  },
  mounted(){
      this.getProvine();
      this.initHandle();
      
  },
  methods: {
    createFormList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '车辆信息'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          initialValue: '',
          readonly: true,
          placeholder: '',
          rules: [{ required: true, message: '请输入VIN', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '电机号',
          fieldName: 'engineCode',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'vhlPlate',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车系',
          fieldName: 'vehicleSeries',
          itemList: [],
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'vehicleType',
          itemList: [],
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '行驶里程',
          fieldName: 'nowMile',
          disabled: true
        },
        {
          type: 'DATE',
          label: '保修起始日期',
          placeholder: '请选择',
          fieldName: 'warrantyStartDate',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          initialValue: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '车主姓名',
          fieldName: 'ownerName',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车主手机号',
          fieldName: 'ownerTel',
          initialValue: '',
          rules: [{ trigger: 'blur', validator: phone }],
          disabled: true
        },
        {
          type: 'BREAK_SPACE',
          label: '救援信息'
        },
        {
          type: 'INPUT',
          label: '救援单号',
          fieldName: 'outNo',
          initialValue: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '报修人',
          fieldName: 'cmsClaimName',
          initialValue: '',
          rules: [{ required: true, message: '请输入报修人', trigger: 'blur' }],
          
        },
        {
          type: 'INPUT',
          label: '报修人电话',
          fieldName: 'cmsClaimTel',
          initialValue: '',
          rules: [{ required: true, message: '请输入报修人电话', trigger: 'blur', validator: phone }]
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'applyDate',
          dateType: 'datetime',
          initialValue: moment().format('YYYY-MM-DD HH:mm:ss'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '故障件代码',
          fieldName: 'partCode',
          initialValue: '',
          placeholder: '',
          readonly: true,
        },
        {
          type: 'INPUT',
          label: '故障件名称',
          fieldName: 'partName',
          initialValue: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '处理结果',
          fieldName: 'dealResult',
          selfCols: 4,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '外出原因',
          fieldName: 'outReason',
          selfCols: 4,
          initialValue: '',
          rules: [{ required: true, message: '请输入外出原因', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '用户意见',
          fieldName: 'opinion',
          selfCols: 4,
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '外出人员',
          fieldName: 'outPerson',
          selfCols: 2,
          initialValue: '',
          rules: [{ required: true, message: '请输入外出人员', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '外出方式',
          fieldName: 'outType',
          itemList: this.createDictList(5538),
          rules: [{ required: true, message: '请输入外出方式', trigger: 'change' }],
        },
        {
          type: 'SELECT',
          label: '是否拖车',
          fieldName: 'isTrailer',
          itemList: this.createDictList(1004),
          rules: [{ required: true, message: '请输入是否拖车', trigger: 'change' }],
        },
        {
          type: 'SELECT',
          label: '出发省份',
          fieldName: 'fromP',
          itemList: [],
        },
        {
          type: 'SELECT',
          label: '出发城市',
          fieldName: 'fromC',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '出发区县',
          fieldName: 'fromA',
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '出发地址',
          fieldName: 'fromAddress',
          selfCols: 4,
          initialValue: '',
          rules: [{ required: true, message: '请输入出发地址', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '故障地省份',
          fieldName: 'toP',
          itemList: [],
          rules: [{ required: true, message: '请输入故障地省份', trigger: 'change' }],
        },
        {
          type: 'SELECT',
          label: '故障地城市',
          fieldName: 'toC',
          itemList: [],
          rules: [{ required: true, message: '请输入故障地城市', trigger: 'change' }],
        },
        {
          type: 'SELECT',
          label: '故障地区县',
          fieldName: 'toA',
          itemList: [],
          rules: [{ required: true, message: '请输入故障地区县', trigger: 'change' }],
        },
        {
          type: 'SELECT',
          label: '是否偏远地区',
          fieldName: 'isFar',
          itemList: this.createDictList(1004),
          rules: [{ required: true, message: '请输入是否偏远地区', trigger: 'change' }],
        },
        {
          type: 'INPUT',
          label: '故障发生地址',
          fieldName: 'toAddress',
          selfCols: 4,
          initialValue: '',
          rules: [{ required: true, message: '请输入故障发生地址', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '外出路线',
          fieldName: 'outLine',
          selfCols: 4,
          initialValue: '',
          rules: [{ required: true, message: '请输入外出路线', trigger: 'blur' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '救援双边里程',
          fieldName: 'outMile',
          rules: [{ required: true, message: '请输入救援双边里程', trigger: 'blur' }],
        },
        {
          type: 'INPUT_NUMBER',
          label: '人数',
          fieldName: 'outPersons',
          rules: [{ required: true, message: '请输入人数', trigger: 'blur' }],
        },
        {
          type: 'RANGE_DATE',
          label: '救援时间',
          fieldName: 'bDate|eDate',
          selfCols: 2,
          placeholder: '请输入',
          dateType: 'datetimerange',
          initialValue: '',
          rules: [{ required: true, message: '请输入救援时间', trigger: 'blur' }],
        },
        {
          type: 'BREAK_SPACE',
          label: '费用信息',
        },
        {
          type: 'INPUT',
          label: '路程奖励',
          fieldName: 'fuelSubsidy',
          rules: [{ required: true, message: '请输入路程奖励', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '人员补贴',
          fieldName: 'prSubsidy',
          rules: [{ required: true, message: '请输入人员补贴', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '餐补',
          fieldName: 'foodSubsidy',
          rules: [{ required: true, message: '请输入餐补', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '住宿补贴',
          fieldName: 'staySubsidy',
          rules: [{ required: true, message: '请输入住宿补贴', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '车船票',
          fieldName: 'crossAmount',
          rules: [{ required: true, message: '请输入车船票', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '拖车费',
          fieldName: 'trailerAmount',
          rules: [{ required: true, message: '请输入拖车费', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '其他费用',
          fieldName: 'otherAmount',
          rules: [{ required: true, message: '请输入其他费用', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '激励类型',
          fieldName: 'rewardType',
          initialValue: '',
          rules: [{ required: true, message: '请输入激励类型', trigger: 'change' }],
          itemList: this.createDictList(5537)
        },
        {
          type: 'INPUT',
          label: '奖励金额',
          fieldName: 'rewardFee',
          rules: [{ required: true, message: '请输入奖励金额', trigger: 'blur' }],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '合计费用',
          fieldName: 'allAmount',
          disabled: true,
          rules: [{ required: true, message: '请输入合计费用', trigger: 'blur' }],
          initialValue: '',
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remarks',
          selfCols: 4,
          initialValue: '',
        },
         {
          type: 'UPLOAD_IMG',
          label: '导航公里数',
          fieldName: 'wayPicture',
          placeholder: '上传导航公里数...',
          selfCols: 1,
          initialValue: [],
          isCompress: false,
          rules: [{ required: true, message: '请上传导航公里数', trigger: 'change' }],
          upload: {
            actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          }
        },
        {
          type: 'UPLOAD_IMG',
          label: '目的地标识',
          fieldName: 'wayPicture2',
          placeholder: '上传目的地标识...',
          selfCols: 1,
          initialValue: [],
          isCompress: false,
          rules: [{ required: true, message: '请上传目的地标识', trigger: 'change' }],
          upload: {
            actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          }
        },
        {
          type: 'UPLOAD_IMG',
          label: '人车合影',
          fieldName: 'wayPicture3',
          placeholder: '上传人车合影...',
          selfCols: 1,
          initialValue: [],
          isCompress: false,
          upload: {
            actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          }
        },
        {
          type: 'UPLOAD_IMG',
          label: '上板车照片',
          fieldName: 'wayPicture4',
          placeholder: '上传上板车照片...',
          selfCols: 1,
          initialValue: [],
          isCompress: false,
          upload: {
            actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          }
        },
        {
          type: 'UPLOAD_IMG',
          label: '铭牌',
          fieldName: 'wayPicture5',
          placeholder: '上传铭牌...',
          selfCols: 1,
          initialValue: [],
          isCompress: false,
          upload: {
            actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          }
        },
        {
          type: 'UPLOAD_IMG',
          label: '公里数',
          fieldName: 'wayPicture6',
          placeholder: '上传公里数...',
          selfCols: 1,
          initialValue: [],
          isCompress: false,
          upload: {
            actionUrl: `${config.uploadUrl}/api/file/oss/upload`,
            fixedSize: [5, 3],
            limit: 1,
            isCalcHeight: true,
            isFileName: true,
            isPreview:true
          }
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件信息',
          fieldName: 'appendixAddress',
          placeholder: '请选择',
          disabled: true,
          initialValue: '',
          selfCols: 4
        },
        {
          type: 'BREAK_SPACE',
          label: '审核信息',
        },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          fieldName: 'checkRemarks',
          selfCols: 4,
          initialValue: '',
        },
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '审核人',
          dataIndex: 'checkBy',
          sorter: true,
        },
        {
          title: '审核时间',
          dataIndex: 'checkAt',
          sorter: true,
        },
        {
          title: '状态',
          dataIndex: 'checkStatus',
          sorter: true,
          dictItems: this.createDictList(5536)
        },
        {
          title: '审核意见',
          dataIndex: 'checkRemarks',
          sorter: true,
        },
      ];
    },
    async updateStatus(type) {
      let checkRemarks = null;
      // 55361003通过 55361004驳回 55361005拒绝
      const [err, data] = await this.$refs.formPanel.GET_FORM_DATA();
      if (err) return;
      if (type === 55361004) {
        // 驳回时，原因必填
        checkRemarks = this.formList.find(item => item.fieldName === 'checkRemarks').initialValue;
        if (!(checkRemarks && checkRemarks.length)) {
          this.$notify.error({ type: '提示信息', message: '驳回需要填写审核描述！！！' });
          return;
        }
      }
      
      let params = { ...data };
      console.log('data',params)
      params.checkRemarks = checkRemarks;
      params.outId = this.formData.outId;
      params.ownerCode = this.formData.ownerCode;
      params.outStatus = type;
      console.log('params',params)
      let res = await assessorStatus(params);
      if (res.resultCode === 200) {
        this.$notify.success({ type: '提示信息', message: '操作成功!!' });
        this.$emit('close', false, 'refresh');
      }
    },
    // 通过
    async passClick() {
      await this.updateStatus(55361003);
    },
    // 驳回
    async turnDownClick() {
      await this.updateStatus(55361004);
    },
    closeDialog() {
      this.$emit('close', false);
    },
    //获取省
    async getProvine() {
      const res = await getProvince();
      this.formList.find(item => item.fieldName === 'fromP').itemList = res.data.map(x => ({ value: x.regionId + '', text: x.regionName }));
      this.formList.find(item => item.fieldName === 'toP').itemList = res.data.map(x => ({ value: x.regionId + '', text: x.regionName }));
    },
    async initHandle(){
      this.formList.forEach(x => {
        x.initialValue = this.formData[x.fieldName];
        x.disabled = true;
        if(this.type === 'edit' && x.fieldName === 'checkRemarks'){
          x.disabled = false;
        }
        x.rules = [];
        x.placeholder = '';
      });
      this.formList.find(item => item.fieldName === 'bDate|eDate').initialValue = [this.formData.bDate,this.formData.eDate];
      if (this.formData.fromP) {
          const res = await getCitys(this.formData.fromP);
          this.formList.find(item => item.fieldName === 'fromC').itemList = res.data.map(x => ({ value: x.regionId + '', text: x.regionName }));
      }
      if (this.formData.toP) {
          const res = await getCitys(this.formData.toP);
          this.formList.find(item => item.fieldName === 'toC').itemList = res.data.map(x => ({ value: x.regionId + '', text: x.regionName }));
      }
      if (this.formData.fromC) {
          const res = await getDistrict(this.formData.fromC);
          this.formList.find(item => item.fieldName === 'fromA').itemList = res.data.map(x => ({ value: x.regionId + '', text: x.regionName }));
      }
      if (this.formData.toC) {
          const res = await getDistrict(this.formData.toC);
          this.formList.find(item => item.fieldName === 'toA').itemList = res.data.map(x => ({ value: x.regionId + '', text: x.regionName }));
      }
      if(this.formData.outType === 55381001){
          this.formList.find(item => item.fieldName === 'crossAmount').hidden = true;
          this.formList.find(item => item.fieldName === 'fuelSubsidy').hidden = false;
      }else if(this.formData.outType === 55381002){
          this.formList.find(item => item.fieldName === 'fuelSubsidy').hidden = true;
          this.formList.find(item => item.fieldName === 'crossAmount').hidden = false;
      }else{
          this.formList.find(item => item.fieldName === 'crossAmount').hidden = false;
          this.formList.find(item => item.fieldName === 'fuelSubsidy').hidden = false;
      }
      //附件
        this.formData.fileList.forEach(async x => {
          if(x.type === 55141001 && x.fileUrl !== null){
            let fileUrl = await this.downLoadByUrl(x.fileUrl);
            this.formList.find(x => x.fieldName == 'wayPicture').initialValue = [fileUrl];
          }
          if(x.type === 55141002 && x.fileUrl !== null){
            let fileUrl = await this.downLoadByUrl(x.fileUrl);
            this.formList.find(x => x.fieldName == 'wayPicture2').initialValue = [fileUrl];
          }
          if(x.type === 55141003 && x.fileUrl !== null){
            let fileUrl = await this.downLoadByUrl(x.fileUrl);
            this.formList.find(x => x.fieldName == 'wayPicture3').initialValue = [fileUrl];
          }
          if(x.type === 55141004 && x.fileUrl !== null){
            let fileUrl = await this.downLoadByUrl(x.fileUrl);
            this.formList.find(x => x.fieldName == 'wayPicture4').initialValue = [fileUrl];
          }
          if(x.type === 55141005 && x.fileUrl !== null){
            let fileUrl = await this.downLoadByUrl(x.fileUrl);
            this.formList.find(x => x.fieldName == 'wayPicture5').initialValue = [fileUrl];
          }
          if(x.type === 55141006 && x.fileUrl !== null){
            let fileUrl = await this.downLoadByUrl(x.fileUrl);
            this.formList.find(x => x.fieldName == 'wayPicture6').initialValue = [fileUrl];
          }
        });
      const res =  await getOutLog({outId: this.formData.outId});
      this.dataList = res.data
    },
    // 获取服务端文件 to blob
    async downLoadByUrl(url, params = {}) {
      let res = await findFileByUrl(url);
      console.log(res.data)
      return res.data;
    },
  }
};
</script>
